<template>
    <div>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.name" placeholder="姓名"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="search">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSale" :disabled="this.sels.length===0">上架</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="offSale" :disabled="this.sels.length===0">下架</el-button>
                </el-form-item>
            </el-form>
        </el-col>
        <!--列表-->
        <el-table :data="products"  highlight-current-row v-loading="listLoading" @selection-change="rowChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="name" label="服务名称"  >
            </el-table-column>
            <el-table-column prop="saleprice" label="销售价"  >
            </el-table-column>
            <el-table-column prop="offsaletime" label="下架"  >
            </el-table-column>
            <el-table-column prop="onsaletime" label="上架"  >
            </el-table-column>
            <el-table-column prop="state" label="状态"  >


            </el-table-column>
            <el-table-column label="操作" width="150">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination  @size-change="handleSizeChange" layout="sizes,prev, pager, next" @current-change="handleCurrentChange" :page-sizes="[5, 10, 15, 20]" :page-size="pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>

        <!--新增界面-->
        <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="服务名称" prop="name">
                    <el-input v-model="addForm.name" ></el-input>
                </el-form-item>
                <el-form-item label="销售价格">
                    <el-input v-model="addForm.saleprice"></el-input>
                </el-form-item>
                <el-form-item label="原价">
                    <el-input v-model="addForm.costprice" ></el-input>
                </el-form-item>
                <el-form-item label="产品图片">
                    <el-upload
                            class="upload-demo"
                            action="/api/common/fastdfsUpload"
                            :on-remove="handleRemove"
                            :on-success="handleSuccess"
                            :file-list="fileList"
                            list-type="picture">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                    </el-upload>
                </el-form-item>

                <el-form-item label="详情">
                    <!--<el-input v-model="addForm.intro" ></el-input>-->
                    <quill-editor v-model="addForm.intro" :options="quillConfig"></quill-editor>
                </el-form-item>
                <el-form-item label="预约须知">
                    <!--<el-input v-model="addForm.orderNotice" ></el-input>-->
                    <quill-editor v-model="addForm.orderNotice" :options="quillConfig"></quill-editor>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    //引入富文本编辑器
    import { quillEditor } from 'vue-quill-editor'
    //相关的样式
    import 'quill/dist/quill.core.css'
    import 'quill/dist/quill.snow.css'
    import 'quill/dist/quill.bubble.css'
    import quillConfig from '../../common/js/quill-config'

    export default {
        components: {
            quillEditor
        },
        name: "employee",
        data(){
            return{
                quillConfig:quillConfig,
                sels:[],
                fileList:[],
                products:[],
                listLoading:true,//加载效果
                filters:{},
                total:0,
                currPage:1,
                pageSize:10,
                addFormVisible:false,
                addForm:{
                    id:null,
                    name:'',
                    saleprice:0,
                    costprice:0,
                    resources:'',
                    intro:'',
                    orderNotice:''

                },
                addLoading:false,
                addFormRules:{//规则详情
                    name: [
                        { required: true, message: '请输入部门名称', trigger: 'blur' },
                        { min: 2, message: '部门名称不能小于两个字符', trigger: 'blur' }
                    ],
                },
            }

        },
        methods:{
            //上架功能
            onSale(){
                var ids = this.sels.map(item => item.id);
                //把用户选中的数据发送到后台
                this.$http.patch("/product",ids).then((res)=>{
                    let{success,message} =res.data;
                    if(success){
                        this.$message({
                            message: message,
                            type: 'success'
                        });
                    }else {
                        this.$message({
                            message: '网络异常',
                            type: 'error'
                        });
                    }
                    //重新加载列表
                    this.loadListData();
                })

            },
            offSale(){

            },
            rowChange(sels){
                this.sels = sels;
            },
            //上传成功的时候的钩子方法
            handleSuccess(response, file, fileList){
                //如果上传多张图片，把多张图片以 , 分割 把图片地址拼接到一起
                //group1/M00/00/F9/wKgABF_eydiAQO5BAAFaMv8X2Ac101.jpg，/group1/M00/00/F9/wKgABF_eydiAQO5BAAFaMv8X2Ac101.jpg，/group1/M00/00/F9/wKgABF_eydiAQO5BAAFaMv8X2Ac101.jpg
                console.debug(fileList)
                let resPath='';
                for(var i =0;i<fileList.length;i++){
                    if (i==fileList.length-1){
                        resPath+=fileList[i].response.resultObj;
                    }else {
                        resPath+=fileList[i].response.resultObj+",";
                    }
                }

                //把响应的路径给 logo字段
               this.addForm.resources=resPath;
            },
            handleRemove(file, fileList) {

            },
            addSubmit(){
                //打开加载效果
                this.addLoading=true;
                this.$refs.addForm.validate((valid) => {
                    if (valid) {//如果通过验证  发起后台的请求添加数据
                        this.addForm.detail={
                            intro:this.addForm.intro,
                            orderNotice:this.addForm.orderNotice
                        }

                        this.$http.put("/product", this.addForm).then((res) => {
                            let {success, message} = res.data;
                            if (success) {
                                this.$message({
                                    message: message,
                                    type: 'success'
                                });
                            } else {
                                this.$message({
                                    message: message,
                                    type: 'error'
                                });
                            }
                            //关闭 模态框
                            this.addFormVisible = false;
                            this.addLoading=false;
                            //重新加载列表
                            this.loadListData();
                        })
                    }

                })
            },
            handleSizeChange(val){//监听用户选择哪一个展示的值
                this.pageSize=val;
                //重新发起请求加载数据
                this.loadListData();
            },
            handleCurrentChange(val){
                this.currPage=val;
                this.loadListData();
            },
            batchRemove(){

            },
            handleAdd(){
                //清空表单
                for( var i in this.addForm){
                      this.addForm[i]='';
                }

                this.fileList=[];
                this.addFormVisible=true;

            },
            search(){
                //把当前页改为第一页
                this.currPage=1
                this.loadListData();

            },
            selsChange(){

            },
            handleEdit(index,row){
                //回显数据
                for( var i in this.addForm){
                    this.addForm[i]=row[i];
                }
                this.addForm.id=row.id;

                this.addForm.intro=row.detail.intro;
                this.addForm.orderNotice=row.detail.orderNotice;

                //详情id
                this.addForm.detail={
                    id:row.detail.id
                }

                //图片的回显
                this.fileList=[];
                var fileArray = row.resources.split(",");
                for(var i=0;i<fileArray.length;i++){
                   this.fileList.push({
                        name:'xxxx.jpg',
                        url:'http://121.37.194.36'+fileArray[i]
                    })
                }


                this.addFormVisible=true;

            },
            handleDel(index,row){
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(()=>{
                    this.listLoading=true;
                    //发起后台的请求  删除数据
                    this.$http.delete("/emp/"+row.id).then((res)=>{
                        //解构表达式
                        let {success,message}=res.data;
                        if(success){
                            this.$message({
                                message: message,
                                type: 'success'
                            });
                        }else {
                            this.$message({
                                message: '网络异常',
                                type: 'error'
                            });
                        }
                        //重新加载列表
                        this.loadListData();
                    })

                })
            },
            loadListData(){
                let param={
                    currentPage:this.currPage,
                    keywords:this.filters.name,
                    pageSize:this.pageSize
                }
                this.$http.post("/product",param).then((res)=>{
                    this.products=res.data.data;
                    this.total=res.data.total;
                })
                this.listLoading=false;

            }

        },
        mounted(){
            this.loadListData();
        }
    }
</script>

<style scoped>

</style>